<?php
namespace app\shops\controller;

use think\Session;
use think\Db;
use think\Model;
use app\plug\controller\qiniu\Qiniu;

/**
 * 投票活动管理
 */
class VoteAct extends Base
{
    /**
     * @throws
     * 投票活动列表
     * @return [type] [description]
     */
    public function index()
    {
        Db("vote_act")->where('end_time','lt',time())->update(['status'=>1]);
        // 获取配置信息
        $conf = config();
        // 分页信息
        $pageSize   = input('limit')    ? input('limit')    : $conf['paginate']['list_rows'];

        // 获取前台传过来的查询条件
        $title = input('get.keywords') ? : "";
        $this->assign('title',$title);
        $pageParam['query']['title'] = $title;

        // 列表显示所有投票活动
        $list = Db("vote_act")->where('shop_id',$this->shop_id)
            ->where('is_del',0)->where('title','like',"%{$title}%")
            ->order("id desc")->paginate($pageSize);


        $this->assign([
            'total'         => $list->total(),
            'page'          => $list->render(),
            'list'          => $list
        ]);

        return $this->fetch();

    }

    /**
     * 添加投票活动
     */
    public function add()
    {

        // 新增投票信息
        if(request()->isAjax()){

            // 图片资源
            $file   = request()->file('pic_url');

            if(!$file){
                $this->error("广告图片未上传");
            }

            // 本地路径
            $filePath = $file->getRealPath();

            // 文件后缀
            $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);

            // 七牛存储文件名
            $file_name = 'images/banner/'.date("Ymd").rand(1000,9999).'.'.$ext;

            // 实例化七牛云
            $qiniu = new Qiniu();

            // 上传至七牛云
            $re = $qiniu->upload_pic($filePath,$file_name);
            if($re['code'] == 0){
                $this->error("图片未成功上传七牛云".$re['data']);
            }

            // 存入数据库
            $data['shop_id']       = $this->shop_id;
            $data['title']         = input('title');
            $data['start_time']    = strtotime(input('start_time'));
            $data['end_time']      = strtotime(input('end_time'));
            $data['vote_limit']    = input('vote_limit');
            $data['total_limit']   = input('total_limit');
            $data['day_limit']     = input('day_limit');
            $data['content']       = input('content');
            $data['pic_url']       = $re["data"];
            $data['create_time']   = time();

            $re = Db("vote_act") -> insert($data);

            if($re){
                $this->success("添加成功");
            }else{
                $this->error("网络错误");
            }

        }

        return $this->fetch();
    }


    /**
     * @throws
     * 编辑投票信息
     */
    public function edit()
    {
        $id = input('id');
        // 更新数据
        if(request()->isAjax()){

            $data = input('');

            // 图片是否被编辑
            $file   = request()->file('pic_url');
            if($file){
                // 本地路径
                $filePath = $file->getRealPath();

                // 文件后缀
                $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);

                // 七牛存储文件名
                $file_name = 'images/vote_player/'.date("Ymd").rand(1000,9999).'.'.$ext;

                // 实例化七牛云
                $qiniu = new Qiniu();

                // 上传至七牛云
                $re = $qiniu->upload_pic($filePath,$file_name);
                if($re['code'] == 0){
                    $this->error("图片未成功上传七牛云".$re['data']);
                }
                $data['pic_url']    = $re["data"];

            }

            input('id')         == null || $data['id']         = input('id');
            input('title')      == null || $data['title']      = input('title');
            input('start_time') == null || $data['start_time'] = strtotime(input('start_time'));
            input('end_time')   == null || $data['end_time']   = strtotime(input('end_time'));
            input('vote_limit') == null || $data['vote_limit'] = input('vote_limit');
            input('total_limit')== null || $data['total_limit']= input('total_limit');
            input('day_limit')  == null || $data['day_limit']  = input('day_limit');
            input('content')    == null || $data['content']    = input('content');


            $res = Db("vote_act") ->where('shop_id',$this->shop_id) ->where('id',$id)->update($data);

            if($res===false){
                $this->error("编辑失败");
            }
            $this->success("编辑成功");

        }

        //拿到投票活动信息
        $info = Db("vote_act")->field('id, title, start_time, end_time, content, vote_limit, total_limit, day_limit,pic_url')
            ->where('shop_id',$this->shop_id) ->where('id',$id)->find();

        $this->assign('info',$info);

        return $this->fetch();
    }


    /**
     * 软删除
     */
    public function del()
    {
        if (request()->isPost()) {
            $id = input('id');
            $res = Db('vote_act')->where('id', $id)->update(['is_del' => 1]);
            if ($res) {
                $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }
        }
    }

    /**
     * 投票记录
     */
    public function act_recode()
    {
        // 获取配置信息
        $conf = config();
        // 分页信息
        $pageSize   = input('limit')    ? input('limit')    : $conf['paginate']['list_rows'];

        // 获取前台传过来的查询条件
        $nickname = input('get.keywords') ? : "";
        $this->assign('nickname',$nickname);
        $pageParam['query']['nickname'] = $nickname;

        // 列表显示所有投票记录
        $list = Db("vote_recode")->where('act_id',input('act_id'))
            ->where('nickname','like',"%{$nickname}%")
            ->order("id desc")->paginate(['query'=>request()->param(),$pageSize]);

        $this->assign([
            'total'         => $list->total(),
            'page'          => $list->render(),
            'list'          => $list
        ]);

        return $this->fetch();
    }


}
